package webbandidong.controller.mobilecontroller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import webbandidong.auth.AuthAdminSimpleFormController;
import webbandidong.bo.CompanyBO;
import webbandidong.bo.MobileBO;
import webbandidong.model.Company;
import webbandidong.model.Mobile;
import webbandidong.util.Constant;
import webbandidong.util.PageInfo;
import webbandidong.viewmodel.SearchMobileViewModel;

public class ShowMobilesListController extends AuthAdminSimpleFormController {

    @Autowired
    private MobileBO mobileBO;

    @Autowired
    private CompanyBO companyBO;

    public void setMobileBO(MobileBO mobileBO) {
        this.mobileBO = mobileBO;
    }

    public void setCompanyBO(CompanyBO companyBO) {
        this.companyBO = companyBO;
    }

    public ShowMobilesListController() {
        setCommandClass(SearchMobileViewModel.class);
        setCommandName("searchMobileViewModel");
        setFormView("admin/manage/showmobiles");
        setSuccessView("admin/manage/showmobiles");
    }

    @Override
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
        return new SearchMobileViewModel();
    }

    @Override
    protected Map referenceData(HttpServletRequest request) throws Exception {
        Map referenceData = new HashMap();

        List<Company> companies = (List<Company>)companyBO.getAllCompanies();
        Map<Integer, String> company = new LinkedHashMap<Integer, String>();
        if (companies != null) {
            for (Company c : companies) {
                company.put(c.getId(), c.getName());
            }
            referenceData.put("companies", company);
        }

        return referenceData;
    }

    @Override
    protected void initBinder(HttpServletRequest request,
            ServletRequestDataBinder binder) throws Exception {
        SimpleDateFormat dataFormat = new SimpleDateFormat("dd/MM/yyyy");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dataFormat, true));
    }

    @Override
    public ModelAndView onAuthSubmit(
            HttpServletRequest request,
            HttpServletResponse response,
            Object command,
            BindException errors) throws Exception {

        int page = 1;
        String pageNum = request.getParameter("page");
        if (pageNum != null && !"".equals(pageNum)) {
            try {
                page = Integer.parseInt(pageNum);
                if (page < 1) {
                    page = 1;
                }
            }
            catch (Exception ex) {
                page = 1;
            }
        }

        SearchMobileViewModel model = (SearchMobileViewModel)command;
        List<Mobile> mobiles = mobileBO.getMobilesCriteria(model, page);

        if (mobiles != null) {
            PageInfo pageInfo = new PageInfo();
            pageInfo.totalItems = mobileBO.getMobilesCriteria(model).size();
            pageInfo.pageSize = Constant.PAGE_SIZE;
            pageInfo.currentPage = page;
            request.setAttribute("pageInfo", pageInfo);
        }

        request.setAttribute("searchMobileViewModel", model);
        request.setAttribute("mobiles", mobiles);
        return showForm(request, response, errors);
    }
}